﻿using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.CentralModels;
using Guline.Web.CentralModels.Object;
using Guline.Web.Central.Controllers;
using Guline.PaymentAPI;
using Newtonsoft.Json.Linq;
using System.Data;

namespace Guline.Web.Central.Areas.Contacts.Controller
{
    public class ContactController : BaseController
    {
        //
        // GET: /Contacts/Contact/
        public ActionResult ImportTxt()
        {
            return View();
        }
        public ActionResult Index()
        {
            return View(db.Contacts.Where(m=>m.Account.UserName==User.Identity.Name).OrderByDescending(m=>m.ID).ToList());
        }
        public ActionResult CreateContact()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreateContact(Contact model)
        {
            if (ModelState.IsValid)
            {
                model.AccountID = db.Accounts.Where(m => m.UserName == User.Identity.Name).FirstOrDefault().ID;
                this.db.Contacts.Add(model);
                this.db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult EidtContact(int ID)
        {
            var contact = db.Contacts.Where(m => m.ID == ID).FirstOrDefault();
            return View(contact);
        }
        [HttpPost]
        public ActionResult EidtContact(Contact model)
        {
            if (ModelState.IsValid)
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }
        public ActionResult DeleteContact(int ID)
        {
            var contact = db.Contacts.Where(m => m.ID == ID).FirstOrDefault();
            db.Contacts.Remove(contact);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


    }
}
